package com.fog.student.controller;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fog.student.common.Base;
import com.fog.student.common.UserContext;
import com.fog.student.dto.CourseDto;
import com.fog.student.entity.Student;
import com.fog.student.service.CourseService;
import com.fog.student.service.StudentService;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;
    @Resource
    private CourseService courseService;
    @GetMapping("/page")
    public Base page(int page , int pageSize , String name){
        Page<Student> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.orderByDesc("username");
        if(name !=null){
            studentQueryWrapper.like("name", name);
        }
        studentService.page(pageInfo, studentQueryWrapper);
        return Base.success(pageInfo);
    }
    @GetMapping("/{id}")
    public Base getStudentById(@PathVariable Long id){
        Student student = studentService.getById(id);
        return Base.success(student);
    }
    @PutMapping
    public Base changeStatusStudent(@RequestBody Student student){
        studentService.saveOrUpdate(student);
        return Base.success("更新成功");
    }

    @PostMapping
    public Base addStudent(HttpServletRequest request, @RequestBody Student student){
        // 1. 查询学生是否储存
        Student one = studentService.getOne(new QueryWrapper<Student>().eq("username", student.getUsername()));
        if (one != null){
            return  Base.error( student.getUsername() + "用户已经存在");
        }
        // 2. 填写创建用户信息，填写初始化密码
        student.setPassword("123456");
        student.setClassId(student.getClassId());
        // 3.存入到数据库
        studentService.save(student);
        return Base.success("添加用户成功");
    }
    @GetMapping("/selectCourse")
    public Base selectCourse(String  ids){
        studentService.selectCourse(ids);
        return Base.success("选课成功");
    }

    @GetMapping("/cancelCourse")
    public Base cancelCourse(String  ids){
        studentService.cancelCourse(ids);
        return Base.success("退课成功");
    }

    @GetMapping("/select")
    public Base selectPage(int page , int pageSize , String name){
        Page<CourseDto> pageInfo = courseService.selectSearch(page,pageSize,name);
        return Base.success(pageInfo);
    }
    @GetMapping("/noselect")
    public Base noSelectPage(int page , int pageSize , String name){
        Page<CourseDto> pageInfo = courseService.noSelectSearch(page,pageSize,name);
        return Base.success(pageInfo);
    }

}
